package com.zhiyou100.api.set_demo;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Demo1 {

    public static void main(String[] args) {
        testSort();
    }

    /**
     * HashSet典型的场景：去重
     * 将list1与list2重复元素去掉，形成新的集合
     */


    /**
     * HashSet 不自带sort排序功能
     * stream.sorted
     */
    public static void testSort(){
        Set<Student> set = new HashSet<>();
        set.add(new Student("jack",20));
        set.add(new Student("jack",20));
        set.add(new Student("rose",21));
        set.add(new Student("lucy",22));
        set.add(new Student("tom",19));
        //排序  (补充)
        //借用java8中的stream对象对set集合中的元素进行排序，将排序好的元素最后放入一个指定集合中
        //此时因为Student是自定义类，需要实现Comparable接口，重写compareTo方法
        Stream<Student> sorted = set.stream().sorted();
        List<Student> list = sorted.collect(Collectors.toList());
        System.out.println("list = " + list);
    }


    /**
     * forEach方法
     */
    public static void testForEach(){
        Set<Student> set = new HashSet<>();
        set.add(new Student("jack",20));
        set.add(new Student("jack",20));
        set.add(new Student("rose",21));
        set.add(new Student("lucy",22));
        set.add(new Student("tom",19));
        //循环元素
        set.forEach(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println("student = " + student);
                student.setAge(student.getAge() + 2);
            }
        });
        System.out.println(set);
        //stream
        set.stream().forEach(student -> System.out.println(student));

    }


    /**
     * HashSet的遍历
     */
    public static void testFor() {
        Set<Student> set = new HashSet<>();
        set.add(new Student("jack",20));
        set.add(new Student("jack",20));
        set.add(new Student("rose",21));
        set.add(new Student("lucy",22));
        set.add(new Student("tom",19));
        //set遍历   foreach
        for (Student stu : set){
            System.out.println(stu);
        }
        System.out.println("------------迭代器方式----------");
        //将set集合转成Iterator对象    快捷键  alt+enter 快速生成返回值
        Iterator<Student> iterator = set.iterator();
        while (iterator.hasNext()){
            Student student = iterator.next();
            System.out.println("student = " + student);
        }
    }


    /**
     * HashSet的操作
     */
    public static void testSet() {
        HashSet<String> set = new HashSet<>();
        String s = "李毅";
        String s2 = new String("李毅");
        //添加元素
        set.add(s);
        set.add("洪超");
        set.add("王冲冲");
        set.add(s2);
        System.out.println("set = " + set);
        //获取当前set集合元素个数
        int size = set.size();
        System.out.println("size = " + size);
        //判断是否包含某个元素
        boolean b = set.contains("洪超");
        System.out.println("b = " + b);
        if (!set.contains("李仁政")) {
            set.add("李仁政");
        }
        //判断当前set集合是否为空
        boolean b1 = set.isEmpty();
        System.out.println("b1 = " + b1);
        //移除元素
        boolean b2 = set.remove("洪超");
        System.out.println("b2 = " + b2);
        //equals  判断两个集合元素是否一致
        HashSet<String> set2 = new HashSet<>();
        boolean b3 = set.equals(set2);
        System.out.println("b3 = " + b3);
        //根据条件移除
        boolean b4 = set.removeIf(new Predicate<String>() {
            //test方法的形参就是此set集合中的元素
            @Override
            public boolean test(String s) {
                //定义移除逻辑
                return s.contains("王");
            }
        });
        System.out.println("b4 = " + b4);
        System.out.println(set);
    }

    /**
     * HashSet集合的创建
     */
    public static void testCreateSet() {
        //创建一个默认大小为16，元素数据类型为String的HashSet集合
        HashSet<String> set = new HashSet<>();
        //创建一个默认大小为20，元素数据类型为String的HashSet集合
        HashSet<String> set2 = new HashSet<>(20);
        //通过一个特定的集合对象创建set集合
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 78, 9, 10, 11, 12, 9);
        HashSet<Integer> set3 = new HashSet<>(list);
        System.out.println("set3 = " + set3);
        //如果有一天，我们目前数据在list集合中，但是需要去重？
    }


}
